package com.yyh.synctool.client.job;

import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;

/**
 * @author xuan
 * @date 2021/12/6
 */
public class TriggerFactory
{
    /** 每30分钟触发一次 */
    public static Trigger getInterval30Minutes() {
        return getByInterval("minute", 30);
    }

    /** 每5分钟触发一次 */
    public static Trigger getInterval5Minutes() {
        return getByInterval("minute", 5);
    }

    /** 每5秒钟触发一次 */
    public static Trigger getInterval5Seconds() {
        return getByInterval("second", 5);
    }

    private static Trigger getByInterval(String type, int interval) {
        SimpleScheduleBuilder simpleScheduleBuilder = null;
        if ("hour".equals(type)) {
            simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInHours(interval)
                    .repeatForever();
        } else if ("minute".equals(type)) {
            simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInMinutes(interval)
                    .repeatForever();
        } else if ("second".equals(type)) {
            simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInSeconds(interval)
                    .repeatForever();
        } else if ("milliseconds".equals(type)){
            simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInMilliseconds(interval)
                    .repeatForever();
        } else {
            simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInSeconds(5)
                    .withRepeatCount(10);
        }

        Trigger trigger = TriggerBuilder.newTrigger()
            .withSchedule(simpleScheduleBuilder)
            .startNow()
            .build();
        return trigger;
    }
}
